package com.whsxt.buss.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whsxt.buss.data.InportData;
import com.whsxt.buss.domain.BusGoods;
import com.whsxt.buss.domain.BusInport;
import com.whsxt.buss.domain.BusProvider;
import com.whsxt.buss.service.IBusGoodsService;
import com.whsxt.buss.service.IBusInportService;
import com.whsxt.buss.service.IBusProviderService;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.PageInfo;
import com.whsxt.common.Result;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 贺炯
 * @since 2019-09-28
 */
@RestController
@RequestMapping("/buss/inport")
public class BusInportController {
	
	@Autowired
	private IBusInportService iBusInportService;
	
	@Autowired
	private IBusGoodsService iBusGoodsService;
	
	@Autowired
	private IBusProviderService iBusProviderService;
	/**
	 * 	进货列表
	 * @param data
	 * @return
	 */
	@RequestMapping("/list")
	public Object list(InportData data) {
		IPage<BusInport> page = new Page<BusInport>(data.getPage(), data.getLimit());
		QueryWrapper<BusInport> queryWrapper = new QueryWrapper<BusInport>();
		queryWrapper.eq(data.getProviderid()!=null&&data.getProviderid()!= ' ', "providerid", data.getProviderid());
		queryWrapper.eq(data.getGoodsid()!=null&&data.getGoodsid()!= ' ', "goodsid", data.getGoodsid());
		queryWrapper.gt(StrUtil.isNotBlank(data.getStartTime()), "inporttime", data.getStartTime());
		queryWrapper.lt(StrUtil.isNotBlank(data.getEndTime()), "inporttime", data.getEndTime());
		iBusInportService.page(page,queryWrapper);
		List<BusInport> records = page.getRecords();
		for (BusInport busInport : records) {
			Integer goodsid = busInport.getGoodsid();
			BusGoods byId = iBusGoodsService.getById(goodsid);
			busInport.setGoodsname(byId.getGoodsname());
			busInport.setSize(byId.getSize());
			Integer providerid = busInport.getProviderid();
			BusProvider byId2 = iBusProviderService.getById(providerid);
			busInport.setProvidername(byId2.getProvidername());
		}
		PageInfo pageInfo = new PageInfo(page.getTotal(), records);
		return new Result(pageInfo);
	}
	/**
	 *  	新增进货
	 * @param inport
	 * @return
	 */
	@RequestMapping("/addInport")
	public Object addInport(BusInport inport) {
		try {
			iBusInportService.addInport(inport);
			return new Result(CodeMsg.INPORT_ADD_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.INPORT_ADD_ERROR);
		}
	}
	
	/**
	 *   更新货物数据
	 * @param inport
	 * @return
	 */
	@RequestMapping("/updateInport")
	public Object updateInport(BusInport inport) {
		try {
			iBusInportService.updateInport(inport);
			return new Result(CodeMsg.INPORT_UPDATE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.INPORT_UPDATE_ERROE);
		}
	}
	 /**
	  *  	删除进货
	  * @param id
	  * @return
	  */
	@RequestMapping("/deleteOne")
	public Object deleteOne(Integer id) {
		try {
			iBusInportService.deleteOne(id);
			return new Result(CodeMsg.INPORT_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.INPORT_DELETE_ERROE);
		}
	}
	/**
	 *  退货处理
	 * @param id
	 * @param number
	 * @param remark
	 * @return
	 */
	@RequestMapping("/back")
	public Object back(Integer id,Integer number,String remark) {
		try {
			iBusInportService.back(id,number,remark);
			return new Result(CodeMsg.INPORT_BACK_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.INPORT_BACK_SECCUSS);
		}
	}
}

